// pages/alarm-detail/alarm-detail.js
const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    userInfo_sj: {},
    /*  */
    status: '',  //案件状态
    /*  */
    backBtnData: [
      {
        name: '返回列表'
      }
    ],
    /*  */
    refuseBtnData:[
      {}
    ],
    /*  */
    confirmBtnData: [
      {}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户信息
    this.setData({
      userInfo_sj: app.globalData.get_userInfo()
    })
    let eventChannel = this.getOpenerEventChannel()
    eventChannel.on('show-alarm-detail', res => {
      console.log(res)
      this.setData({
        id: res.info.id,
        status: res.info.status
      })
      wx.setNavigationBarTitle({
        title: `案件详情-${res.info.statusDesc}`,
      })
      this.getDefendedInfo(res.info.id)
    })
  },
  /* 放弃操作 */
  refuseDefendedBack: function(e){
    let { index } = e.currentTarget.dataset
    app.globalData.setNeedAnimation.call(this, 'refuseBtnData', this.data.refuseBtnData, index).then(() => {
      if(this.data.status=='0'){
        //this.updateDefend('-1')
      }
      if(this.data.status=='1'){
        wx.showModal({
          title:'提示',
          content:'确定客户已放弃维修吗',
          confirmColor:app.globalData.modalConfirmColor,
          success: res => {
            if(res.confirm){
              this.updateDefend('-1')
            }
          }
        })
      }
    })
  },
  /* 确认操作 */
  confirmDefendedBack: function(e){
    let { index } = e.currentTarget.dataset
    app.globalData.setNeedAnimation.call(this, 'confirmBtnData', this.data.confirmBtnData, index).then(() => {
      if(this.data.status=='0'){
        wx.showModal({
          title:'提示',
          content:'确定客户已到店吗',
          confirmColor:app.globalData.modalConfirmColor,
          success: res => {
            if(res.confirm){
              this.updateDefend('1')
            }
          }
        })
      }
      if(this.data.status=='1'){
        wx.showModal({
          title:'提示',
          content:'确定客户要开始维修吗',
          confirmColor:app.globalData.modalConfirmColor,
          success: res => {
            if(res.confirm){
              this.updateDefend('3')
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getDefendedInfo: function (defendedid) {
    wx.showLoading({
      title: '获取案件详情',
      mask: true
    })
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.Url + '/wx/getDefendedInfo',
        method: 'POST',
        header: this.data.userInfo_sj.header,
        data: util.json2Form({
          id: defendedid
        }),   //
        success: res => {
          //console.log(res)
          //token失效
          if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
          if (res.data.code == '1') {
            /* 无数据 */
            this.setData({
              id: defendedid,
              name: res.data.body.name,
              mobile: res.data.body.mobile,
              licenseNo: res.data.body.licenseNo,
              carvin: res.data.body.carvin,
              engineNo: res.data.body.engineNo,
              appealTime: res.data.body.appealTime,
              address: res.data.body.address,
              caseMobile: res.data.body.caseMobile,
              explore: res.data.body.explore,
              exploreMobile: res.data.body.exploreMobile
            })
            resolve()
          } else {
            wx.showToast({
              icon: 'none',
              title: '' + res.data.msg
            })
            reject()
          }
        },
        fail: err => {
          console.log(err)
          wx.showToast({
            icon: 'none',
            title: '获取数据失败'
          })
          reject()
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },
  defendedBack: function (e) {
    let { index } = e.currentTarget.dataset
    app.globalData.setNeedAnimation.call(this, 'backBtnData', this.data.backBtnData, index).then(() => {
      var pages = getCurrentPages(); //当前页面
      var beforePage = pages[pages.length - 2]; //前一页
      wx.navigateBack({
        success: function () {
          //beforePage.onLoad(); // 执行前一个页面的onLoad方法
        }
      })
    })
  },
  /* 修改案件状态 */
  updateDefend: function (status) {
    wx.showLoading({
      title: '提交中',
      mask: true
    })
    wx.request({
      url: app.globalData.Url + '/wx/updateDefended',
      method: 'POST',
      header: this.data.userInfo_sj.header,
      data: util.json2Form({
        id: this.data.id,
        status: status
      }),   //
      success: res => {
        //console.log(res)
        //token失效
        if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
        if (res.data.code == '1') {
          wx.showToast({
            title: '' + res.data.msg,
            complete:()=>{
              wx.navigateTo({
                url: '/pages/alarm/alarm',
                success: res=>{
                  res.eventChannel.emit('back-to-alarm-list',{
                    backFrom:'alarm-detail'
                  })
                }
              })}
            })
        } else {
          wx.showToast({
            icon: 'none',
            title: '' + res.data.msg
          })
        }
      },
      fail: err => {
        console.log(err)
        wx.showToast({
          icon: 'none',
          title: '请求接口失败'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  }
})